package com.myhome.money.dubbo.consumer;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.service.GenericService;
import com.myhome.money.dubbo.CallbackListener;
import com.myhome.money.dubbo.CallbackService;
import com.myhome.money.dubbo.DemoService;
import com.myhome.money.dubbo.pojo.Student;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: 许路路
 * @Date: 2019/8/20
 */
@Component
public class FooConsumer {

    @Reference(url = "dubbo://127.0.0.1:28866", interfaceName = "com.myhome.money.dubbo.FooService", generic = true)
    private GenericService genericService;

    @Reference(url = "dubbo://127.0.0.1:28866", interfaceName = "com.myhome.money.dubbo.CallbackService")
    private CallbackService callbackService;

    @Reference(protocol = "dubbo", interfaceName = "com.myhome.money.dubbo.DemoService")
    private DemoService demoService;

    public String hello() {
        Object object = genericService.$invoke("sayHi", new String[]{"java.lang.String"}, new Object[]{"xululu"});
        return object.toString();
    }

    public String getStudent() {
        Student student = new Student();
        student.setId(2);
        student.setBirth(new Date());
        student.setName("xululu");
        student.setStuNo("20190809");
        student.setPayable(5.6);
        Object object = genericService.$invoke("getStudent", new String[]{"com.myhome.money.dubbo.pojo.Student"}, new Object[]{student});
        return object.toString();
    }

    public void demo() {
        System.out.println("demo:" + new Date());
        demoService.demo("hello");
    }

    public void callBack() {
        callbackService.addListener("rod.xu", new CallbackListener() {
            @Override
            public void changed(String msg) {
                System.out.println("callback:" + msg);
            }
        });
    }

}
